(function(){"use strict";BX.namespace("BX.Landing");var n="undo";var t="redo";var i="init";var o="resolved";var e="pending";var r=100;var a=BX.Landing.Utils.isPlainObject;var d=BX.Landing.Utils.bind;var s=BX.Landing.Utils.fireCustomEvent;BX.Landing.History=function(){this.stack=[];this.commands={};this.position=-1;this.state=i;this.commandState=o;this.onStorage=this.onStorage.bind(this);d(window,"storage",this.onStorage);c(this).then(l).then(m).then(y)};BX.Landing.History.instance=null;BX.Landing.History.getInstance=function(){var n=BX.Landing.PageObject.getRootWindow();if(!n.BX.Landing.History.instance){n.BX.Landing.History.instance=new BX.Landing.History}return n.BX.Landing.History.instance};function c(i){i.registerCommand(new BX.Landing.History.Command({id:"editText",undo:BX.Landing.History.Action.editText.bind(null,n),redo:BX.Landing.History.Action.editText.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"editEmbed",undo:BX.Landing.History.Action.editEmbed.bind(null,n),redo:BX.Landing.History.Action.editEmbed.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"editMap",undo:BX.Landing.History.Action.editMap.bind(null,n),redo:BX.Landing.History.Action.editMap.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"editImage",undo:BX.Landing.History.Action.editImage.bind(null,n),redo:BX.Landing.History.Action.editImage.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"editIcon",undo:BX.Landing.History.Action.editIcon.bind(null,n),redo:BX.Landing.History.Action.editIcon.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"editLink",undo:BX.Landing.History.Action.editLink.bind(null,n),redo:BX.Landing.History.Action.editLink.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"sortBlock",undo:BX.Landing.History.Action.sortBlock.bind(null,n),redo:BX.Landing.History.Action.sortBlock.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"addBlock",undo:BX.Landing.History.Action.removeBlock.bind(null,n),redo:BX.Landing.History.Action.addBlock.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"removeBlock",undo:BX.Landing.History.Action.addBlock.bind(null,n),redo:BX.Landing.History.Action.removeBlock.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"updateStyle",undo:BX.Landing.History.Action.editStyle.bind(null,n),redo:BX.Landing.History.Action.editStyle.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"addCard",undo:BX.Landing.History.Action.removeCard.bind(null,n),redo:BX.Landing.History.Action.addCard.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"removeCard",undo:BX.Landing.History.Action.addCard.bind(null,n),redo:BX.Landing.History.Action.removeCard.bind(null,t)}));i.registerCommand(new BX.Landing.History.Command({id:"updateBlockState",undo:BX.Landing.History.Action.updateBlockState.bind(null,n),redo:BX.Landing.History.Action.updateBlockState.bind(null,t)}));return Promise.resolve(i)}function u(n){return new Promise(function(t){var i=new Worker("/bitrix/js/landing/history/worker/json-parse-worker.js");i.postMessage(n);i.addEventListener("message",function(n){t(n.data)})})}function g(n){return new Promise(function(t){var i=new Worker("/bitrix/js/landing/history/worker/json-stringify-worker.js");i.postMessage(n);i.addEventListener("message",function(n){t(n.data)})})}function l(n){var t;try{t=BX.Landing.Main.getInstance().id}catch(n){t=-1}return u(window.localStorage.history).then(function(n){return a(n)&&t in n?n[t]:Promise.reject()}).then(function(t){Object.keys(t.stack).forEach(function(i,o){n.stack.push(new BX.Landing.History.Entry(t.stack[i]));if(o>=r){n.stack.shift()}});n.position=Math.min(parseInt(t.position),n.stack.length-1);n.state=t.state;return n}).catch(function(){return n})}function m(n){var t;try{t=BX.Landing.Main.getInstance().id}catch(n){t=-1}return u(window.localStorage.history).then(function(n){return a(n)?n:{}}).then(function(i){i[t]={};i[t].stack=n.stack;i[t].position=n.position;i[t].state=n.state;return i}).then(g).then(function(t){window.localStorage.history=t;return n})}function h(n){n.stack=[];n.position=-1;n.state=i;n.commandState=o;return Promise.resolve(n)}function B(n,t){return u(window.localStorage.history).then(function(n){return a(n)?n:{}}).then(function(t){if(n in t){delete t[n]}return t}).then(g).then(function(n){window.localStorage.history=n;return t})}function f(i,r){if(i.commandState===e){return Promise.resolve(i)}var a=i.position+r;var d=i.state;if(r<0&&i.state!==n){a+=1;d=n}if(r>0&&i.state!==t){a-=1;d=t}if(a<=i.stack.length-1&&a>=0){i.position=a;i.state=d;var s=i.stack[a];if(s){var c=i.commands[s.command];if(c){i.commandState=e;return c[d](s).then(function(){i.commandState=o;return i}).catch(function(){i.commandState=o;return i[d===n?"undo":"redo"]()})}}}return Promise.resolve(i)}function y(n){var t=BX.Landing.PageObject.getRootWindow();s(t.window,"BX.Landing.History:init",[n]);return Promise.resolve(n)}function L(n){var t=BX.Landing.PageObject.getRootWindow();s(t.window,"BX.Landing.History:update",[n]);return Promise.resolve(n)}function X(n){var t=BX.Landing.PageObject.getRootWindow();s(t.window,"BX.Landing.History:actualize",[n]);return Promise.resolve(n)}function H(n){var t=BX.Landing.PageObject.getRootWindow();s(t.window,"BX.Landing.History:newBranch",[n]);return Promise.resolve(n)}function w(n,t){return Promise.resolve(t)}function v(n,t){var i={blocks:[],images:[]};n.forEach(function(n){if(n.command==="addBlock"){i.blocks.push(n.block)}if(n.command==="editImage"){i.images.push({block:n.block,id:n.redo.id})}});return Promise.resolve(i)}BX.Landing.History.prototype={undo:function(){if(this.canUndo()){return f(this,-1).then(m).then(L)}return Promise.resolve(this)},redo:function(){if(this.canRedo()){return f(this,1).then(m).then(L)}return Promise.resolve(this)},canUndo:function(){return this.position>0&&this.state===t||this.position>0&&this.state===n||this.position===0&&this.state!==n},canRedo:function(){return this.position<this.stack.length-1&&this.state!==i||this.position!==-1&&this.position===this.stack.length-1&&this.state!==t},push:function(i){var o=this.position+1;var e=this.stack.length;if(this.state===n){o-=1}var a=this.stack.splice(o,e,i);if(this.stack.length>r){a.push(this.stack.shift())}if(a.length){this.onNewBranch(a)}this.position=this.stack.length-1;this.state=t;m(this).then(L)},registerCommand:function(n){if(n instanceof BX.Landing.History.Command){this.commands[n.id]=n}},removePageHistory:function(n){return B(n,this).then(function(t){var i;try{i=BX.Landing.Main.getInstance().id}catch(n){i=-1}if(i===n){return h(t)}return Promise.reject()}).then(L).catch(function(){})},onStorage:function(n){if(n.key===null){if(!window.localStorage.history){h(this).then(L)}}},onNewBranch:function(n){return v(n,this).then(function(n){return w(n,this)}.bind(this))}}})();
//# sourceMappingURL=history.map.js